package v1alpha1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type RedisChaosSpec struct {
	// ContainerSelector specifies the target for injection
	ContainerSelector `json:",inline"`

	// Duration represents the duration of the chaos
	// +optional
	Duration *string `json:"duration,omitempty"`

	Action RedisAction `json:"action"`

	// Address Redis地址，如127.0.0.1:6379
	// +optional
	Address string `json:"address"`
	// Password Redis密码
	// +optional
	Password string `json:"password"`
	// Key 过期影响的键，留空表示全部键
	// +optional
	Key string `json:"key"`
	// Option 设置过期的前提条件，NX,XX,GT,LT，分别表示当前无过期、当前有过期、过期变大、过期变小时设置，留空表示始终设置
	// +optional
	Option string `json:"option"`
	// Expiry 新的过期时间，比如1m
	// +optional
	Expiry string `json:"expiry"`
	// Percent 限制的内存百分比，0表示不限制
	// +optional
	Percent int `json:"percent"`
	// Size 限制内存大小，比如256MB，为空表示不限制
	// +optional
	Size string `json:"size"`

	// RemoteCluster represents the remote cluster where the chaos will be deployed
	// +optional
	RemoteCluster string `json:"remoteCluster,omitempty"`
}

type RedisAction string

// expire, limit
const (
	RedisActionExpire RedisAction = "expire"
	RedisActionLimit  RedisAction = "limit"
)

// +kubebuilder:object:root=true
// +kubebuilder:printcolumn:name="action",type=string,JSONPath=`.spec.action`
// +kubebuilder:printcolumn:name="duration",type=string,JSONPath=`.spec.duration`
// +chaos-mesh:experiment

type RedisChaos struct {
	metav1.TypeMeta   `json:",inline"`
	metav1.ObjectMeta `json:"metadata,omitempty"`

	Spec   RedisChaosSpec             `json:"spec"`
	Status BladeOnlyCreateChaosStatus `json:"status,omitempty"`
}

var _ InnerObjectWithCustomStatus = (*RedisChaos)(nil)
var _ InnerObjectWithSelector = (*RedisChaos)(nil)
var _ InnerObject = (*RedisChaos)(nil)

func init() {
	SchemeBuilder.Register(&RedisChaos{}, &RedisChaosList{})
}

// GetSelectorSpecs is a getter for selectors
func (obj *RedisChaos) GetSelectorSpecs() map[string]interface{} {
	return map[string]interface{}{
		".": &obj.Spec.ContainerSelector,
	}
}

func (obj *RedisChaos) GetCustomStatus() interface{} {
	return &obj.Status.BladeOnlyCreateChaosCustomStatus
}
